package com.sofly.view;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.sofly.entity.PurchaseList;
import com.sofly.entity.Suppliers;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
@Component
public class ViewPDF extends AbstractPdfView {

	@Override
	protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String fileName = "采购清单.pdf"; // 设置response方式,使执行此controller时候自动出现下载页面,而非直接使用excel打开
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition",
                "attachment; filename=" + new String(fileName.getBytes(), "iso8859-1"));
        PurchaseList purchaseList =   (PurchaseList) model.get("purchaseList");
        Suppliers suppliers = (Suppliers) model.get("suppliers");
        BaseFont bfChinese = BaseFont.createFont("C://Windows//Fonts//simfang.ttf", BaseFont.IDENTITY_H,
                BaseFont.NOT_EMBEDDED);

        com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 20, com.lowagie.text.Font.NORMAL);
        Table table = new Table(2);        
        table.setBorderWidth(1);    
        table.setBorderColor(Color.BLACK);    
        table.setPadding(2);    
        table.setSpacing(0);        
        table.addCell(new Paragraph("订单号", FontChinese));    
        table.addCell(new Paragraph(purchaseList.getPurchaseId(), FontChinese));    
        table.addCell(new Paragraph("产品编号", FontChinese));    
        table.addCell(new Paragraph(purchaseList.getProductId(), FontChinese));    
        table.addCell(new Paragraph("产品名称", FontChinese));    
        table.addCell(new Paragraph(purchaseList.getProductName(), FontChinese));
        table.addCell(new Paragraph("产品类型", FontChinese));    
        table.addCell(new Paragraph(purchaseList.getProductType(), FontChinese));
        table.addCell(new Paragraph("数量", FontChinese));    
        table.addCell(new Paragraph(purchaseList.getProductNumber()+"", FontChinese));    
        table.addCell(new Paragraph("单位", FontChinese)); 
        table.addCell(new Paragraph(purchaseList.getProductPieces(), FontChinese)); 
        table.addCell(new Paragraph("单价", FontChinese)); 
        table.addCell(new Paragraph(purchaseList.getProductPrices()+"(元)", FontChinese)); 
        table.addCell(new Paragraph("采购人员", FontChinese)); 
        table.addCell(new Paragraph(purchaseList.getPurchaser(), FontChinese)); 
        table.addCell(new Paragraph("采购时间", FontChinese));
        table.addCell(new Paragraph(purchaseList.getPurchasedate(), FontChinese));
        table.addCell(new Paragraph("到货时间", FontChinese));
        table.addCell(new Paragraph(purchaseList.getDealine(), FontChinese));
        table.addCell(new Paragraph("下单时间", FontChinese));
        table.addCell(new Paragraph(purchaseList.getSaveDate(), FontChinese));
        table.addCell(new Paragraph("审批人", FontChinese));
        table.addCell(new Paragraph(purchaseList.getPurchaseLister(), FontChinese));
        table.addCell(new Paragraph("备注", FontChinese));
        table.addCell(new Paragraph(purchaseList.getRemark(), FontChinese));
        table.addCell(new Paragraph("供应商名称", FontChinese));
        table.addCell(new Paragraph(suppliers.getSupplierName(), FontChinese));
        table.addCell(new Paragraph("供应商电话", FontChinese));
        table.addCell(new Paragraph(suppliers.getPhone(), FontChinese));
        table.addCell(new Paragraph("供应商地址", FontChinese));
        table.addCell(new Paragraph(suppliers.getAddress(), FontChinese));
        String value = "               神华集团采购部物料采购单";
        document.add(new Paragraph(value, FontChinese)); 
        value ="     流水号:"+randomUtil();
        document.add(new Paragraph(value, FontChinese)); 
        document.add(table);                  
	}	
	 public String randomUtil(){
		String string1 = "0123456789";
		Random random = new Random(getViewerPreferences());
		String string2 ="";
		for (int i = 0; i <5; i++) {
			string2 += string1.charAt(random.nextInt(string1.length()));
		}
		return "SL"+string2+new SimpleDateFormat("yyyyMMdd").format(new Date());
	 }

}

